/************************
 * 用户登录认证相关接口
 ************************/
import http from "@/utils/http";
import storage from "@/utils/storage";
import encrypt from "@/utils/encrypt"

const LOGON_API= '/belazy-auth/auth/login'
const LOGOUT_API= '/belazy-auth/auth/logout'

export default {
    /**
     * 发送登录短信验证码
     **/
    sendCheckCode: function (param) {
        return http.get("/belazy-auth/auth/sendLoginSmsCode", param,true,true);
    },
    /**
     * 登录
     **/
    login:function (data){
        var ll = this.$CONFIG.APP_NAME
        console.log(ll)

        var form = {
            username:data.username,
            source:'Web',
            smsCode:data.smsCode,
            password:encrypt.RSA.encrypt(data.password),
            grantType:data.grantType
        }
        return new Promise((resolve, reject) => {
            http.post(LOGON_API,form,true,true).then(res=>{
                //存储用户信息
                var accessToken = res.accessToken;
                var tokenType = res.tokenType;
                storage.setLoginInfo(res.userInfo)
                storage.setToken(tokenType+" "+ accessToken)

                resolve(res)
            }).catch((err)=>{
                reject (err)
            })
        })
    },
    /**
     * 登出
     **/
    logout:function (){
      http.post(LOGOUT_API,{},true,true).then(res=>{
          storage.clearSession()
      })
    }
}